<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from "echarts";
import { debounce } from "@/utils";

require("echarts/theme/macarons"); // echarts theme

const animationDuration = 6000;

export default {
  props: {
    className: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "300px"
    }
  },
  data() {
    return {
      chart: null
    };
  },
  mounted() {
    this.initChart();
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize();
      }
    }, 100);
    window.addEventListener("resize", this.__resizeHandler);
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    window.removeEventListener("resize", this.__resizeHandler);
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");

      this.chart.setOption({
        color: ["#38a0ff", "#4cca73"],
        tooltip: {
          trigger: "axis",
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: "shadow" // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        legend: {
          orient: "horizontal",
          x: "center",
          y: "bottom",
          bottom: 10
        },
        grid: {
          top: "2%",
          left: "2%",
          right: "2%",
          bottom: 20,
          containLabel: true
        },
        xAxis: [
          {
            type: "category",
            data: [
              "企业1",
              "企业2",
              "企业3",
              "企业4",
              "企业5",
              "企业6",
              "企业7",
              "..."
            ],
            axisTick: {
              alignWithLabel: true
            }
          }
        ],
        yAxis: [
          {
            type: "value",
            axisTick: {
              show: false
            }
          }
        ],
        series: [
          {
            name: "上个月",
            type: "bar",
            // stack: 'vistors',
            barWidth: "20",
            data: [7, 6.9, 9.5, 14.5, 18.4, 21.5, 25.2, 26.5],
            animationDuration
          },
          {
            name: "这个月",
            type: "bar",
            // stack: 'vistors',
            barWidth: "20",
            data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17, 16.6],
            animationDuration
          }
          // {
          //   name: 'pageC',
          //   type: 'bar',
          //   // stack: 'vistors',
          //   barWidth: '20',
          //   data: [30, 52, 200, 334, 390, 330, 220],
          //   animationDuration
          // }
        ]
      });
    }
  }
};
</script>
